package com.example.dongha.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.dongha.R;
import com.example.dongha.adapter.CountRecyclerAdapter;
import com.example.dongha.adapter.SpaceItemDecoration;
import com.example.dongha.bar.BarChartView;
import com.example.dongha.bar.listener.OnScaleChangeListener;

import java.util.ArrayList;
import java.util.Random;

/**
 * Created by liangkun on 2018/8/8.
 * 作用:综合统计
 */

public class TotalCountFragment extends Fragment {

    private RecyclerView recyclerViewOne;
    private NestedScrollView scrollView;
    private ArrayList<String> strings;
    private BarChartView barChartView;
    private TextView textView;
    private ArrayList<Integer> list; //假数据

    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.total_count_fragment, null);
        recyclerViewOne = view.findViewById(R.id.rv_date);
        barChartView = view.findViewById(R.id.bar_chart);
        textView = view.findViewById(R.id.tv_data);
        scrollView = view.findViewById(R.id.scroll_view);
        getData();
        initData();

        return view;
    }


    private void getData() {
        list = new ArrayList<>();
        for (int i = 0; i < 101; i++) {
            Random rand = new Random();
            int next = rand.nextInt(180);
            list.add(next);
        }

    }

    private void initData() {

        barChartView.setMin(0);
        barChartView.setMax(29);
        barChartView.setSpace(1f);
        barChartView.setCurrent(29);
        //设置数据
        barChartView.setData(list);
        strings = new ArrayList<>();
        String s1 = "昨夜斗回北，今朝岁起东";
        String s2 = "我年已强仕，无禄尚忧农。";
        String s3 = "桑野就耕父，荷锄随牧童。";
        String s4 = " 田家占气候，共说此年丰";
        String s5 = "昨夜斗回北，今朝岁起东";
        String s6 = "迳直夫何细！桥危可免扶？";
        String s7 = "远山枫外淡，破屋麦边孤。";
        String s8 = "宿草春风又，新阡去岁无。";
        String s9 = "梨花自寒食，进节只愁余。";
        String s10 = "昨夜斗回北，今朝岁起东";
        strings.add(s1);
        strings.add(s2);
        strings.add(s3);
        strings.add(s4);
        strings.add(s5);
        strings.add(s6);
        strings.add(s7);
        strings.add(s8);
        strings.add(s9);
        strings.add(s10);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        recyclerViewOne.setNestedScrollingEnabled(false);
        recyclerViewOne.setLayoutManager(new LinearLayoutManager(getActivity()));
        SpaceItemDecoration space = new SpaceItemDecoration(25);
        recyclerViewOne.addItemDecoration(space);
        CountRecyclerAdapter adapterOne = new CountRecyclerAdapter(getActivity(), strings);
        recyclerViewOne.setAdapter(adapterOne);
        scrollView.smoothScrollTo(0, 0);
        barChartView.setOnScaleChangeListener(new OnScaleChangeListener() {
            @Override
            public void onScaleChange(float scale) {
               textView.setText(String.valueOf(list.get((int) scale)));
            }
        });

    }
}
